import { Global } from "./Global";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.ProgressBar)
    Time: cc.ProgressBar = null;

    @property(cc.Label)
    lblTime: cc.Label = null;

    @property(cc.Label)
    Question: cc.Label = null;

    @property([cc.Label])
    AnswerList: cc.Label[] = [];

    @property(cc.SpriteAtlas)
    LifeStateAtlas: cc.SpriteAtlas = null;

    @property(cc.Node)
    LifeState: cc.Node = null;

    @property(cc.Node)
    JiFen: cc.Node = null;

    time_daojishi: number = 120;//倒计时间
    currentQuestion: number = 0;
    currentQuestionList = [1];//数组为空开启随机加载
    currentAnswerOk: string = '';
    currentLifeState: number = 0;
    currentJiFen: number = 0;
    isLoading: boolean = false;

    @property(cc.Node)
    WuKong: cc.Node = null;
    @property(cc.Node)
    Niu: cc.Node = null;


    //悟空进场音效
    @property({
        url: cc.AudioClip,
        displayName: "悟空进场音效"
    })
    audioWukongIn: string = '';


    @property({
        url: cc.AudioClip,
        displayName: "牛魔王进场音效"
    })
    audioNiuIn: string = '';


    @property({
        url: cc.AudioClip,
        displayName: "悟空攻击音效"
    })
    audioGongJi: string = '';

    @property({
        url: cc.AudioClip,
        displayName: "牛魔王被攻击音效"
    })
    audioBeiGongJi: string = '';


    @property({
        url: cc.AudioClip,
        displayName: "牛魔王攻击音效"
    })
    audioNiuGongJi: string = '';


    @property({
        url: cc.AudioClip,
        displayName: "悟空被攻击的音效"
    })
    audioWukongBeiGongJi: string = '';


    @property(Global)
    global: Global = null;
    // onLoad () {},

    start() {
        this.randomSort();
        let action_wukong = cc.moveTo(0.6, cc.p(-440, -70));
        this.WuKong.runAction(action_wukong);
        cc.audioEngine.play(this.audioWukongIn, false, 1);
        cc.audioEngine.play(this.audioNiuIn, false, 1);

        this.onTime();

        if (this.currentQuestionList.length != 0) {
            this.loadQuestion(this.currentQuestion);
        }
        else {
            this.loadQuestion(this.currentQuestionList[this.currentQuestion]);
        }
        this.AnswerList.forEach(ele => {
            ele.node.on("touchstart", this.onSelectAnswer, this);
            ele.node.on(cc.Node.EventType.MOUSE_ENTER, this.global.setCursor, this);
            ele.node.on(cc.Node.EventType.MOUSE_LEAVE, this.global.clearCursor, this);
        })

        this.Niu.cascadeOpacity = false;//节点的不透明度值是否影响其子节点，默认值为 true。 

        let anim_niu0 = this.Niu.getComponent(cc.Animation);
        anim_niu0.play("nui0");
    }
    onTime() {
        this.schedule(this.onDaoJiShi, 1);
    }
    onDaoJiShi() {
        this.time_daojishi--;
        this.lblTime.string = this.time_daojishi.toString();
        this.Time.progress -= 0.0083;
        cc.log(this.Time.progress);
        if (this.time_daojishi == 0) {
            this.unschedule(this.onDaoJiShi);
            // cc.log("游戏超时结束");
            cc.director.loadScene("EndFail");
        }
    }
    loadQuestion(currentQuestion: number) {
        cc.loader.loadRes("subject", (err, res) => {
            if (err) {
                cc.log(err);
            } else {
                this.Question.string = res[currentQuestion].question;
                this.AnswerList.forEach((ele, index) => {
                    ele.string = res[currentQuestion].answer[index];
                })
                this.currentAnswerOk = res[currentQuestion].answerOk;
            }
        })
    }

    onSelectAnswer(event) {
        if (this.isLoading) return false;
        this.isLoading = true;
        let selectAnswerText = event.target.getComponent(cc.Label).string;
        if (selectAnswerText == this.currentAnswerOk) {
            cc.log("回答正确");
            this.currentJiFen += 20;
            this.randomSort();
            this.currentQuestion++;
            this.onSetJiFen(this.currentJiFen);
        } else {
            cc.log("回答错误");
            this.currentLifeState++;
            this.onSetLifeState(this.currentLifeState);
        }
    }
    //设置生命状态(回答错误)
    onSetLifeState(index: number) {
        let anim_niu = this.Niu.getComponent(cc.Animation);
        anim_niu.play("niugongjiwukong");
        this.scheduleOnce(function () {
            cc.audioEngine.play(this.audioNiuGongJi, false, 1);
        }, 0.8);
        this.scheduleOnce(function () {
            cc.audioEngine.play(this.audioWukongBeiGongJi, false, 1);
        }, 1.5);
        anim_niu.once("finished", function () {

            if (this.currentLifeState == 3) {
                cc.director.loadScene("EndFail");
            } else {
                let sp = this.LifeState.getComponent(cc.Sprite);
                sp.spriteFrame = this.LifeStateAtlas.getSpriteFrame(index.toString());
                this.isLoading = false;
            }
        }, this)

    }

    //设置积分（回答正确）
    onSetJiFen(jifen: number) {
        //悟空攻击
        let anim_wukonggongji = this.WuKong.getComponent(cc.Animation);
        anim_wukonggongji.play("wukonggj");
        this.scheduleOnce(function () {
            cc.audioEngine.play(this.audioGongJi, false, 1);

        }, 0.5)

        this.scheduleOnce(function () {
            cc.audioEngine.play(this.audioBeiGongJi, false, 1);
        }, 0.8)

        anim_wukonggongji.once("finished", function () {
            if (this.currentJiFen == 100) {
                cc.director.loadScene("EndWin");
            } else {
                //顺序加载开关判断
                if (this.currentQuestionList.length != 0) {
                    this.loadQuestion(this.currentQuestion);
                }
                else {
                    this.loadQuestion(this.currentQuestionList[this.currentQuestion]);
                }
                this.JiFen.getComponent(cc.Label).string = jifen.toString();
                this.isLoading = false;
            }

        }, this)

    }
    randomSort() {
        if (this.currentQuestionList.length == 5) {
            return;
        }
        let b = Math.round(Math.random() * 4);
        if (this.currentQuestionList.indexOf(b) == -1) {
            this.currentQuestionList.push(b);
        } else {
            this.randomSort();
        }
    }
    onDestroy() {
        this.global.clearCursor();
    }
}
